using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Winform1
{
    public partial class ConnectDialog : Form
    {
        public ConnectDialog()
        {
            InitializeComponent();
        }

        //----
        private string m_hostName = "localhost";
        private int m_portNumber = 23;

        //----
        public string HostName
        {
            get { return m_hostName; }
            set { m_hostName = value; }
        }

        public int PortNumber
        {
            get { return m_portNumber; }
            set { m_portNumber = value; }
        }

        //----
        private void ConnectDialog_Shown(object sender, EventArgs e)
        {
            textBoxHost.Text = m_hostName;
            textBoxPort.Text = m_portNumber.ToString(System.Globalization.CultureInfo.InvariantCulture);
            textBoxHost.Focus();
            textBoxHost.SelectAll(); // make it easy for the user to overtype
        }

        private void ConnectDialog_Validating(object sender, CancelEventArgs e)
        {
            if (!this.ValidateChildren()) {
                e.Cancel = true;
            }
        }

        private void textBoxHost_Validating(object sender, CancelEventArgs e)
        {
            string txt = textBoxHost.Text;
            if (string.IsNullOrEmpty(txt)) {
                e.Cancel = true;
            }
        }

        private void textBoxPort_Validating(object sender, CancelEventArgs e)
        {
            string txt = textBoxPort.Text;
            bool valid;
            UInt16 tmp;
            valid = UInt16.TryParse(txt, out tmp);
            valid = valid && (tmp != 0);
            if (!valid) {
                e.Cancel = true;
            }
        }

        private void ConnectDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!e.Cancel) {
                m_hostName = textBoxHost.Text;
                m_portNumber = UInt16.Parse(textBoxPort.Text);
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

    }
}